package com.apes.fn.scm.crossCompany.transfer.model;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.crossCompany.transfer.model
 * @Author: linwz
 * @CreateTime: 2021-04-07 16:33
 * @Description: 跨公司调拨
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CrossCompanyTransferNew.CONST_TABLE_NAME)
public class CrossCompanyTransferNew extends Model {
    public static final String CONST_TABLE_NAME = "cross_company_tran_new";
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "跨公司调拨单", prefix = "DB%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;

    @Column(name = "source", length = 30)
    @Comment("上游单据")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("上游单据类型")
    private String sourceBusinessType;

    @Column(name = "source_business_type_name", length = 64)
    @Comment("上游单据类型名称")
    private String sourceBusinessTypeName;

    @Column(name = "special_documents", length = 30)
    @Comment("特殊单据")
    private String specialDocuments;

    @Column(name = "special_documents_type", length = 30)
    @Comment("特殊单据类型")
    private String specialDocumentsType;


    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ship_location_id", referencedColumnName = "id")
    @Comment("发货仓库")
    private Location shipLocation;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "receipt_location_id", referencedColumnName = "id")
    @Comment("收货仓库")
    private Location receiptLocation;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "receipt_company_id", referencedColumnName = "id")
    @Comment("收货公司")
    private PartyRole receiptCompany;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "move_type", referencedColumnName = "id")
    @Comment("原因")
    private Enumeration returnReason;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "sent", name = "新建"),
            @State(id = "apply", name = "提交"),
            @State(id = "approve", name = "审核"),
            @State(id = "done", name = "完成"),
            @State(id = "delete", name = "删除")})
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "date_approve")
    @Comment("审批日期")
    private Date dateApprove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "apply_uid", referencedColumnName = "id")
    @Comment("申请人")
    private PartyRole applyUid;

    @Column(name = "date_apply")
    @Comment("申请日期")
    private Date dateApply;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "stop_uid", referencedColumnName = "id")
    @Comment("终止人")
    private PartyRole stopUid;

    @Column(name = "stop_date")
    @Comment("终止日期")
    private Date stopDate;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "print_number", columnDefinition = "number(4,0) default 0")
    @Comment("打印次数")
    private double printNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    @Comment("创建人")
    private PartyRole createUid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_expand_id", referencedColumnName = "id")
    @Comment("会员扩展表")
    private FnCustomerExpand customerExpand;

    @OneToMany(mappedBy = "crossCompanyTransfer", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CrossCompanyTransferItemNew> crossCompanyTransferItems = new HashSet<>();

    public Set<CrossCompanyTransferItemNew> getCrossCompanyTransferItems() {
        for (CrossCompanyTransferItemNew item : crossCompanyTransferItems) {
            item.setCrossCompanyTransfer(this);
        }
        return crossCompanyTransferItems;
    }

    public PartyRole getReceiptCompany() {
        return Objects.isNull(receiptCompany)? (Objects.isNull(receiptLocation)? null :receiptLocation.getCompany()): receiptCompany;
    }

    @Transient
    private boolean empowermentToNoEmpowerment; // 赋能 --> 非赋能
    public boolean getEmpowermentToNoEmpowerment() {
        return this.empowermentToNoEmpowerment;
    }


    @Transient
    private boolean existsTransferProductSupplierIsNull; // 赋能 --> 非赋能 是否获取全部供应商
    public boolean getExistsTransferProductSupplierIsNull() {
        return this.existsTransferProductSupplierIsNull;
    }

    @Transient
    private List<String> transferProductSuppliers; // [赋能 --> 非赋能] 调拨范围供应商
    public List<String> getTransferProductSuppliers() {
        return this.transferProductSuppliers;
    }

    @Transient
    private boolean isAuto = false; // 是否自动产生单据

    @Transient
    private double withDrawDiveryQtySum = 0;

    public double getWithDrawDiveryQtySum() {
        return this.withDrawDiveryQtySum;
    }

    @Transient
    private boolean hideTerminationButton = true;

    public boolean getHideTerminationButton() {
        CrossCompanyTransferItemNew itemNew = this.getCrossCompanyTransferItems().stream().filter(item -> item.getProductSum() - item.getShipNum() - item.getFinishSum() > 0 && this.getState().equals("approve")).findFirst().orElse(null);
        if (itemNew != null) return false;
        return this.hideTerminationButton;
    }

    @Transient
    private String batchRequireId; // 要货批量转储要货单号集合

    @Transient
    private Date printDate; // 打印时使用

    public Date getPrintDate() {
        return new Date();
    }


//    @Transient
//    private double printNumber = 0;//打印次数
}
